public with sharing class FileUploader {

    public PageReference edit() {
        return null;
    }

        
 public string nameFile{get;set;}
    
    //To hold the content of Uplodaed file
    public Blob contentFile{get;set;}
    
    //To hold the Lines of the File, should be seprated by new line character /n
    String[] filelines = new String[]{};
    
    //To hold the list of accounts that needs to be uploaded
    List<Account> accstoupload;
    
    //To hold the List of all accounts
    public List<Account> allAccounts {get;set;}
    
    /**
    <P> This method will read the file and upload the file content in the data base</P>
    */
    public Pagereference ReadFile()
    {
        if(!validate()){
        
                return null;
        }
    
        System.debug('Started');
        nameFile=contentFile.toString();
        System.debug('nameFile found');
        filelines = nameFile.split('\n');
        if(null!=filelines){
                System.debug('Number of lines in the file: ' + filelines.size());
        }
        accstoupload = new List<Account>();
        for (Integer i=1;i<filelines.size();i++)
        {
            String[] inputvalues = new String[]{};
            inputvalues = filelines[i].split(',');
            
            Account a = new Account();
            a.Name = inputvalues[0];
            a.ShippingStreet = inputvalues[1];       
            a.ShippingCity = inputvalues[2];
            a.ShippingState = inputvalues[3];
            a.ShippingPostalCode = inputvalues[4];
            a.ShippingCountry = inputvalues[5];

            accstoupload.add(a);
        }
        try{
        insert accstoupload;
        }
        catch (Exception e)
        {
            ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured. Please check the template or try again later');
            ApexPages.addMessage(errormsg);
        }    
        return null;
    }
    
    public List<Account> getuploadedAccounts()
    {
        if (accstoupload!= NULL)
            if (accstoupload.size() > 0)
                return accstoupload;
            else
                return null;                    
        else
            return null;
    }
    
    /**
    <P> This method is used the validate the input data</P>
    @Return: Return the Boolean value (true/false) which govern the success or failure of validation
    */
    private Boolean validate(){
        List<Apexpages.Message> messages= new List<Apexpages.Message>();
        Boolean isValid=true;
        validateFileUploaded(isValid);
        System.debug(' isValid='+ isValid);
        return isValid;
    }
    
    /**
    <P> 
    */
    
    private Boolean validateFileUploaded(Boolean isValid){
                if(null==nameFile ){
                        System.debug('Isvalid:='+ isValid);
                MessaageUtils.addMessage(ApexPages.severity.ERROR, 'Please select the file to upload First');
                MessaageUtils.addErrorMessage('Please select the file to upload First1');
                MessaageUtils.addWarningMessage('Please select the file to upload First2');
                MessaageUtils.addConfirmMessage('Please select the file to upload First3');
                MessaageUtils.addFatalMessage('Please select the file to upload First4');
                MessaageUtils.addInfoMessage('Please select the file to upload First5');
                isValid=false;
            
          
        }
        System.debug('Added Error message setting isValid='+ isValid);
        return isValid;
    }
       
   public FileUploader()
    { 
        DatabaseObject dbObj= new DatabaseObject();
        dbObj.setCriteriaObjectName('Account');
        dbObj.setFieldToSelect(new Set<String>{'id', 'name', 'ShippingStreet', 'ShippingCity', 'ShippingState', 'ShippingPostalCode', 'ShippingCountry'});
        
        allAccounts=DbUtils.fetchRecords(dbObj);
        String query='select id, name, ShippingStreet, ShippingCity, ShippingState, ShippingPostalCode, ShippingCountry from Account';
        //allAccounts= DbUtils.fireQuery(query);
      // allAccounts=[select id, name, ShippingStreet, ShippingCity, ShippingState, ShippingPostalCode, ShippingCountry from Account ];
    } 
}